package il.ac.tau.cg.ex3.entities.scene.lights;

import il.ac.tau.cg.ex3.entities.styles.Color;
import il.ac.tau.cg.ex3.math.Point;
import il.ac.tau.cg.ex3.math.Vector;

public class SpotLight extends Light
{
	private Point position;
	private Vector direction;
	
	private double att0;
	private double att1;
	private double att2;

	public SpotLight(Color color, Point position, Vector direction, double att0, double att1, double att2)
	{
		super(color);
		
		this.position = position;
		
		this.direction = direction;
		
		this.att0 = att0;
		this.att1 = att1;
		this.att2 = att2;
	}

	@Override
	public Color getIntensity(Point point)
	{
		Vector lightDir = new Vector(position, point);
		
		double cosAlpha     = direction.dot(lightDir);
		double attentuation = getAttenuation(point);
		
		double r = getColor().getR() * cosAlpha / attentuation;
		double g = getColor().getG() * cosAlpha / attentuation;
		double b = getColor().getB() * cosAlpha / attentuation;
		
		Color color = new Color(r, g, b);
		
		return color;
	}
	
	@Override
	public Vector getDirection(Point point)
	{
		return getDirection().normalize();
	}
	
	public Point getPosition()
	{
		return position;
	}

	public void setPosition(Point position)
	{
		this.position = position;
	}

	public Vector getDirection()
	{
		return direction;
	}

	public void setDirection(Vector direction)
	{
		this.direction = direction;
	}

	public double getAtt0()
	{
		return att0;
	}

	public void setAtt0(double att0)
	{
		this.att0 = att0;
	}

	public double getAtt1()
	{
		return att1;
	}

	public void setAtt1(double att1)
	{
		this.att1 = att1;
	}

	public double getAtt2()
	{
		return att2;
	}

	public void setAtt2(double att2)
	{
		this.att2 = att2;
	}

	private double getDistance(Point destPoint)
	{
		Vector dir = new Vector(position, destPoint);

		return dir.length();
	}

	private double getAttenuation(Point destPoint)
	{
		double distance = getDistance(destPoint);

		double attentuation = att0 + att1 * distance + att2 * Math.pow(distance, 2);

		return attentuation;
	}
}
